package GUI;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import Background.Contestant;
import Background.Judge;
import Background.Lists;

/**
 * A class to display the registration window in the gui.  Builds the window and displays it.
 * 
 * @author Chris Petcher
 * @version 13 February, 2013
 *
 */
@SuppressWarnings("serial")
public class SignInGUI extends JFrame
{
  
	/**
	 * A dimension used in setting the size of the frame.
	 */
	
	private static final Dimension MY_SIZE = new Dimension(1000, 500);

	/**
	  * This field is for the Border Layout that contains all my buttons
	  * for the information stage.
	  */

	public JPanel MY_INFO_PANEL;
	
	/**
	  * This field creates a flow layout which hold the top buttons in the frame.
	  */

	public Container MY_GRID;
	
	/**
	 * A container to handle the flow layout.
	 */
	
	public Container MY_FLOW;
	
	/**
	 * A text field for holding the user's username.
	 */
	
	public JTextField MY_USER_FIELD;
	
	/**
	 * A text field for holding the password.
	 */
	
	public JPasswordField MY_PASS_FIELD;
	
	/**
	 * A checkbox that the user can select to indicate they are a contestant.
	 */
	
	private JCheckBox MY_CONTESTANT;
	
	/**
	 * A checkbox that the user can select to indicate they are a judge.
	 */
	
	private JCheckBox MY_JUDGE;
	
	/**
	 * A checkbox that the user can select to indicate they are the organizer.
	 */
	
	private JCheckBox MY_ORGANIZER;

	/**
	 * A list to hold the strings of the user's information.
	 */
	
	public ArrayList<String> MY_LIST;
	
	/**
	 * A boolean to indicate if the user is a contestant.
	 */
	
	public static boolean isContestant = false;
	
	/**
	 * A boolean to indicate if the user is a judge.
	 */
	
	public static boolean isJudge = false;
	
	/**
	 * A boolean that is flipped on when a user was found in the userlist.
	 */
	
	public static boolean isFound = false;
	
	/**
	 * A contestant object.
	 */
	
	static Contestant current_contestant;
	
	/**
	 * 
	 */
	
	private Main_GUI gui;
	
	/**
	 * Constructor for the RegistrationGUI.  Sets the name of the window.
	 */
	
	public SignInGUI(final Main_GUI the_gui) {
		 
	    super("Sign-in");
	    MY_INFO_PANEL = new JPanel(new BorderLayout());
		MY_GRID = new JPanel(new GridLayout(8,1));
		MY_FLOW = new JPanel(new FlowLayout());
		MY_USER_FIELD = new JTextField();
		MY_PASS_FIELD = new JPasswordField();
		MY_CONTESTANT = new JCheckBox("Contestant");
		MY_JUDGE = new JCheckBox("Judge");
		MY_ORGANIZER = new JCheckBox("Organizer");
		MY_LIST = new ArrayList<String>();
		gui = the_gui;
	  }

	/**
	 * 	Method that brings up the sign-in window and sets up the GUI.
	 */
		public void start() {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		    setVisible(true);
		    setPreferredSize(MY_SIZE);
		    createInfoGrid();
		    add(MY_INFO_PANEL);
		    validate();
		    pack();
		}

		/**
		 * A method to display the registration window after it's been closed.
		 */
		
		public void displaySignIn(){
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		    setPreferredSize(MY_SIZE);
		    add(MY_INFO_PANEL);
		    setVisible(true);
		    pack();
		    
		}
		
		/**
		 * Creates the window for the information page.  This creates some labels and text
		 * fields that will capture the user's entered data.
		 */
		
	public void createInfoGrid() {
		final JButton sign_in = new JButton("Sign-in");
		sign_in.addActionListener(new SignInListener());
		MY_GRID.add(new JLabel("Username:"));
		MY_GRID.add(MY_USER_FIELD);
		MY_GRID.add(new JLabel("Password:"));
		MY_GRID.add(MY_PASS_FIELD);
		MY_GRID.add(new JLabel("Type of User"));
		MY_GRID.add(MY_CONTESTANT);
		MY_GRID.add(MY_JUDGE);
		MY_GRID.add(MY_ORGANIZER);
		MY_FLOW.add(MY_GRID, BorderLayout.CENTER);
		MY_INFO_PANEL.add(MY_FLOW);
		MY_INFO_PANEL.add(sign_in, BorderLayout.SOUTH);
		
	}
	
	public void resetSignIn() {
		MY_USER_FIELD.setText("");
		MY_PASS_FIELD.setText("");
		MY_CONTESTANT.setSelected(false);
		MY_JUDGE.setSelected(false);
		MY_ORGANIZER.setSelected(false);
   	  	remove(MY_INFO_PANEL);
   	  	validate();
		
	}
	
	/**
	 * Getter for the current contestant.
	 */
	
	public static Contestant getCurrentContestant(){
		return current_contestant;
	}

	/**
	 * Inner class to go to the next window in the registration process when
	 * button is pressed.
	 * @author Justin Sorrell
	 */
	class SignInListener implements ActionListener {

		/**
		 * Action performed method that displays the about message.
		 * @param the_event The event, ignored.
		 */
		@Override
		public void actionPerformed(final ActionEvent the_event) {
			char[] temp;
			SignInGUI s = Main_GUI.getSignInGUI();
			String username = MY_USER_FIELD.getText();
		    temp = MY_PASS_FIELD.getPassword();
		    String password = new String(temp);
			String name = null;
			boolean success = false;
			boolean blank = false;
			
			if(username.equals("") || password.equals("")){
				blank = true;
				JOptionPane.showMessageDialog(MY_INFO_PANEL, "One or more fields left blank.");
			}
			
			if (!blank) {

				// Contestant
				if (MY_CONTESTANT.isSelected() && !MY_JUDGE.isSelected()
						&& !MY_ORGANIZER.isSelected()) {
					ArrayList<Contestant> contestantList = Lists.getContestantList();
					Contestant selected_contestant = null;//////////////////////////////////
					for (Contestant c : contestantList) {
						if (c.getUserName().equals(username)
								&& c.getPassword().equals(password)) {
							c.setActive();
							current_contestant = c;
							name = c.getName();
							selected_contestant = c;////////////////////////////////////////
							System.out.println("Registration Number: " + c.getRegistrationNumber());
							isFound = true;
							break;
						}// close inner if
					}// close for loop

					if (isFound) {
						JOptionPane.showMessageDialog(null, "Welcome back, " + name + 
								". You are now signed in.");
						success = true;
						Main_GUI.Sign_in.setText("Signed in as: " + username);
						Main_GUI.Sign_in.setEnabled(false);
						Main_GUI.register.setEnabled(false);
						Main_GUI.Registration.setEnabled(false);
						Main_GUI.createEntryButton();
						Main_GUI.createEditButton(selected_contestant);
						Main_GUI.createSignOutButton(name);
						
						/*
						 * Create a method that creates a button on the side called edit information
						 * that also needs parameters to pass in the current location on the list and 
						 * edit the info and call Lists.end() to save the information. Also the button 
						 * needs to be disabled and set visible to false once you hit sign out. 
						 * 
						 * also might need to keep a counter for 
						 */
						
						
						
						
					}// close if

					else {
						JOptionPane
								.showMessageDialog(
										MY_INFO_PANEL,
										"Username and/or password did not"
												+ " match with any registered contestants.");
					}// close else
				}// Close contestant block

				// Judge
				else if (!MY_CONTESTANT.isSelected() && MY_JUDGE.isSelected()
						&& !MY_ORGANIZER.isSelected()) {
					ArrayList<Judge> judgeList = Lists.getJudgeList();

					for (Judge j : judgeList) {
						if (j.getUserName().equals(username)
								&& j.getPassword().equals(password)) {
							j.setActive();
							name = j.getName();
							isFound = true;
						}
					}
					if (isFound) {
						JOptionPane.showMessageDialog(null, "Welcome back, "
								+ name + ". You are now signed in.");
						success = true;
						Main_GUI.Sign_in.setText("Signed in as: " + username);
						Main_GUI.Sign_in.setEnabled(false);
						Main_GUI.Registration.setEnabled(false);
						Main_GUI.createEntryListButton();
						Main_GUI.createSelectWinnersButton();
						Main_GUI.createSignOutButton(name);
						
					} else {
						JOptionPane
								.showMessageDialog(MY_INFO_PANEL,
										"Username and/or password did not match with any registered judges.");
					}
				}// close judge block

				// Organizer
				else if (!MY_CONTESTANT.isSelected() && !MY_JUDGE.isSelected()
						&& MY_ORGANIZER.isSelected()) {

					if (MY_USER_FIELD.getText().equals("jbowman")
							&& password.equals("password")) {
						isFound = true;
					}

					if (isFound) {
						JOptionPane
								.showMessageDialog(null,
										"Welcome back, Mrs. Bowman. You are now signed in.");
						success = true;
						Main_GUI.Sign_in.setText("Signed in as: " + username);
						Main_GUI.Sign_in.setEnabled(false);
						Main_GUI.Registration.setEnabled(false);
						Main_GUI.createContestantListButton();
						Main_GUI.createJudgeListButton();
						Main_GUI.createEntryListButton();
						Main_GUI.createSignOutButton("Mrs. Bowman");
					}

					else {
						JOptionPane.showMessageDialog(MY_INFO_PANEL,
								"Username and/or password did not"
										+ " match with the organizer.");
					}// close else

				}// close organizer block

				// only one of the checkboxes should be selected
				else {
					JOptionPane.showMessageDialog(MY_INFO_PANEL,
							"You must select only one of the valid options.");
				}
			}
			if (success) {
				resetSignIn();
				s.dispose();
				validate();
				gui.repaint();
				
			}

		}// close actionperformed

	}// close listener class

}// close SignInGUI class